#!/usr/bin/python
#coding: utf-8

"""
把每个人的基本信息写入到一个文件夹下，
写入信息包括：

jpg图片
个人头像

txt文件
用户名
年龄
地址
代言商品
"""

import re
import os
import os.path
import urllib2

__author__ = u"吴修树"

class TBMM(object):

    def __init__(self):
        self.connect = r"C:\\Users\\root\\Desktop\\淘宝MM\\"
        self.siteURL = 'http://mm.taobao.com/json/request_top_list.htm'
        # 防止访问识别
        self.headers = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
           'Referer' : 'http://mm.taobao.com/json/request_top_list.htm'}
        self.res = []

    # 爬取网页
    def getPage(self, getPageIndex):
        # print getPageIndex
        url = self.siteURL + "?page=" + str(getPageIndex)
        # print url
        request = urllib2.Request(url, headers = self.headers)
        response = urllib2.urlopen(request)
        return response.read().decode("gbk").encode("utf-8")

    # 正则匹配
    def getContents(self, getPageIndex):
        page = self.getPage(getPageIndex)
        # print len(page)
        pattern = re.compile(r'<div class="pic s60".*?<img src="//(.*?)".*?<p class="top".*?<a.*?>(.*?)</a>.*?<em>.*?<strong>(.*?)</strong>.*?<span>(.*?)</span>.*?<p class="description">(.*?)</p>', re.S)
        items = re.findall(pattern, page)
        for index, item in enumerate(items):
            x = "https://" + item[0]
            self.res.append([x, item[1], item[2], item[3], item[4]])
            # print self.connect.decode("utf-8")
            self.createMakdir(index, self.connect.decode("utf-8") + item[1].decode("utf-8") + r"\\")
            
    # 创建文件夹
    def createMakdir(self, pageIndex, path):
        path = path.strip()
        # 判断路径是否存在
        # 存在 True
        # 不存在 False
        isExists = os.path.exists(path)
        if not isExists:
            # 如果不存在则创建目录
            # 创建目录操作函数
            os.makedirs(path)
        # 保存图片
        result = self.res[pageIndex]
        self.saveImg(result[0].decode("utf-8"), path + result[1].decode("utf-8") + ".jpg")

        # 保存基本信息
        self.saveBrief(path + result[1].decode("utf-8") + ".txt", result[1], result[2], result[3], result[4].strip())
        

    #传入图片地址，文件名，保存单张图片
    def saveImg(self,imageURL,fileName):
        # print fileName
        picture = urllib2.urlopen(imageURL)
        # 读取图片的数据
        data = picture.read()
        with open(fileName, "wb") as f:
            f.write(data)

    def saveBrief(self, fileName, name, age, address, brief):
        # print fileName
        with open(fileName, "w+") as f: 
            f.write("姓名：" + name + "\n")
            f.write("年龄：" + age + "\n")
            f.write("住址：" + address + "\n")
            f.write("代言产品：" + brief + "\n")

def main():
    tbmm = TBMM()
    flag = True
    while True:
        try:
            st = "请输入要爬取的页码(输入为空结束)："
            x = raw_input(st.decode("utf-8").encode("gbk"))
            if len(x.strip()) == 0:
                flag = False
                break
            # 判断输入是否合法
            x = int(x)
            tbmm.getContents(x)
            flag = True
        except Exception as e:
            print e
            print u"输入出错"
            flag = False
        finally:
            if flag == True:
                print u"读取成功！！！"
                st = "输入任意键继续"
                x = raw_input(st.decode("utf-8").encode("gbk"))
                del x

if __name__ == "__main__":
    # 测试编码
    # t = TBMM()
    # t.getContents(1)

    main()